package com.tj.admin.rest;

import com.fqgj.common.api.Page;
import com.tj.admin.auth.exceptions.UploadRequest;
import com.tj.admin.auth.response.UploadDTO;
import com.tj.admin.auth.service.UploadApplication;
import com.tj.admin.auth.vo.PictureResponseVO;
import com.tj.bo.common.conf.api.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/file")
public class UploadController {
    @Autowired
    private UploadApplication uploadApplication;
    /**
     * 上传图片
     * @param request
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    public ApiResponse<PictureResponseVO> uploadPicture(HttpServletRequest request) {
        String url = uploadApplication.uploadExcel(request);
        PictureResponseVO pictureResponseVO = new PictureResponseVO(url);
        return new ApiResponse<>(pictureResponseVO).setMsg("upload success");
    }

    /**
     * 上传图片
     * @param request
     * @return
     */

    @RequestMapping(value = "/save")
    public ApiResponse<Boolean> saveFileInfo(@RequestBody UploadRequest request) {
        Boolean info = uploadApplication.saveFileInfo(request.getUrl(), request.getContent());
        return new ApiResponse<>(info).setMsg("Save Success");
    }


    /**
     * 上传图片
     * @param page
     * @return
     */
    @RequestMapping(value = "/get")
    public ApiResponse<UploadDTO> getFileInfoList(Page page) {
        ApiResponse<UploadDTO> response = uploadApplication.selectUploadInfoList(page);
        return response;
    }
}
